package reservation.flight;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class Function {
	
	 InputStream is =null;
	public String conexion(ArrayList<NameValuePair> nameValuePairs ){
		String result = "";
		 //http post
        try{
                HttpClient httpclient = new DefaultHttpClient();
                //HttpPost httppost = new HttpPost("http://10.0.2.2/conexion/");
                HttpPost httppost = new HttpPost("http://www.jvtecgroup.com/conexionAndroid.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                 is = entity.getContent();
                 Log.i("log_tag", "Realizo conexion ");
        }catch(Exception e){
                Log.e("log_tag", "Error en conexion http "+e.toString());
        }
        //convert response to string
        try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                }
                is.close();
                
                result=sb.toString();
                
                Log.i("log_tag", "Realizo conversion de resultados "+ result);
        }catch(Exception e){
                Log.e("log_tag", "Error convertir resultados "+e.toString());
        }
         
        
        return result;
	}
	public int loginUsuario(String result){
		int res=0;
		//parse json data
        try{
      	  	JSONArray jArray = new JSONArray(result);
                for(int i=1;i<jArray.length();i++){
                        JSONObject json_data = jArray.getJSONObject(i);
                       
                        Log.i("log_tag","total: "+json_data.getInt("total")                             
                        );
                        res= json_data.getInt("total");
                }
                
                Log.i("log_tag", "Realizo pasar datos "+ res);
        }catch(JSONException e){
                Log.e("log_tag", "Error pasar datos "+e.toString());
        }
        return res;
	}
	public int develverIdUsuario(String result){
		int res=0;
		//parse json data
        try{
      	  	JSONArray jArray = new JSONArray(result);
                for(int i=1;i<jArray.length();i++){
                        JSONObject json_data = jArray.getJSONObject(i);
                       
                        Log.i("log_tag","id: "+json_data.getInt("id")                             
                        );
                        res= json_data.getInt("id");
                }
                
                Log.i("log_tag", "Realizo pasar datos "+ res);
        }catch(JSONException e){
                Log.e("log_tag", "Error pasar datos "+e.toString());
        }
        return res;
	}
	public String[] llenarpais(String result){
		String[] items;
		//parse json data
        try{
      	  	JSONArray jArray = new JSONArray(result);
      	  	items=new String[jArray.length()-1];
                for(int i=1;i<jArray.length();i++){
                        JSONObject json_data = jArray.getJSONObject(i);                       
                        //Log.i("log_tag","total: "+json_data.getString("name"));
                        items[i-1]=json_data.getString("name");
                }                
                Log.i("log_tag", "Realizo pasar datos Pais "+ items.length);
                
                return items;
        }catch(JSONException e){
                Log.e("log_tag", "Error pasar datos "+e.toString());
        }
		return items=null;
	}
	public boolean guardarReservacion(String result){
		int res=0;
		try{
      	  	JSONArray jArray = new JSONArray(result);
                for(int i=1;i<jArray.length();i++){
                        JSONObject json_data = jArray.getJSONObject(i);
                       
                        Log.i("log_tag","total: "+json_data.getInt("total")                             
                        );
                        res= json_data.getInt("total");
                        Log.i("log_tag", "Realizo pasar datos "+ res);
                        if(res!=0)
                        	return true;
                }
                
                
        }catch(JSONException e){
                Log.e("log_tag", "Error pasar datos "+e.toString());
        }
		
		return false;
	}
	public ReservationTitle[] devolverMisReservacion(String result){
		Log.e("Log","Entro a mis reservaciones ");
		ReservationTitle[] items;
		String origen, destino, salida, regreso;
		//parse json data
        try{
      	  	JSONArray jArray = new JSONArray(result);
      	  	items=new ReservationTitle[jArray.length()-1];
                for(int i=1;i<jArray.length();i++){
                        JSONObject json_data = jArray.getJSONObject(i);           
                        origen=json_data.getString("origen");
                        destino=json_data.getString("destino");
                        salida=json_data.getString("salida");
                        regreso=json_data.getString("regreso");
                        items[i-1]=new ReservationTitle(origen, destino, salida, regreso);
                        
                }                
                Log.i("log_tag", "Realizo pasar datos Reservaciones "+ items.length);
                
                return items;
        }catch(JSONException e){
                Log.e("log_tag", "Error pasar datos "+e.toString());
        }
		return null;
	}

	public boolean comprobarExistenciaUsuario(String result){
		int res=0;
		 Log.i("log_tag", "String.. "+result);
		try{
      	  	JSONArray jArray = new JSONArray(result);
                for(int i=1;i<jArray.length();i++){
                        JSONObject json_data = jArray.getJSONObject(i);                       
                        Log.i("log_tag","total: "+json_data.getInt("total")  );
                        res= json_data.getInt("total");
                        Log.i("log_tag", "Realizo pasar datos "+ res);
                        if(res==0)
                        	return true;
                }               
                
        }catch(JSONException e){
                Log.e("log_tag", "Error pasar datos "+e.toString());
        }
		return false;
	}
	public boolean guardarUsuario(String result){
		int res=0;
		Log.e("log_tag", "Guardar Usuario....!");
		try{
      	  	JSONArray jArray = new JSONArray(result);
                for(int i=1;i<jArray.length();i++){
                        JSONObject json_data = jArray.getJSONObject(i);
                       
                        Log.i("log_tag","total: "+json_data.getInt("total")                             
                        );
                        res= json_data.getInt("total");
                        Log.i("log_tag", "Realizo pasar datos.. Guardo usuario"+ res);
                        if(res!=0)
                        	return true;
                }
                
                
        }catch(JSONException e){
                Log.e("log_tag", "Error pasar datos "+e.toString());
        }
		
		return false;
	}
	
	public int Revisar_Pais(String result){
		int res=0;
		//parse json data
        try{
      	  	JSONArray jArray = new JSONArray(result);
                for(int i=1;i<jArray.length();i++){
                        JSONObject json_data = jArray.getJSONObject(i);
                       
                        Log.i("log_tag","total: "+json_data.getInt("total")                             
                        );
                        res= json_data.getInt("total");
                }
                
                Log.i("log_tag", "Realizo pasar datos "+ res);
        }catch(JSONException e){
                Log.e("log_tag", "Error pasar datos "+e.toString());
        }
        return res;
	}
	
}
